﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.RoleMenuCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.RoleMenuHandler
{
    public class AddRangeRoleMenuHandler : IRequestHandler<AddRangeRoleMenuCommand, APIResult<int>>
    {
        private readonly IBaseRepository<RoleMenu> rmRep;
        private readonly IMapper mapper;

        public AddRangeRoleMenuHandler(IBaseRepository<RoleMenu> rmRep,IMapper mapper)
        {
            this.rmRep = rmRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(AddRangeRoleMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var roleMenus = request.MenuIds
                .Select(menuId => new RoleMenu { MenuId = menuId, RoleId = request.RoleId }).ToList();

            rmRep.AddRanges(roleMenus);
            result.Code = APIEnum.Success;
            result.Msg = "批量添加成功";
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
